package net.easyjava.framework.utils;

import net.easyjava.framework.log.LogConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ProjectProperties extends Properties {
	private static Logger logger = LoggerFactory.getLogger(ProjectProperties.class);


	private static final long serialVersionUID = 7282643438764720232L;

	private ProjectProperties(String filePath){
		try {
//			InputStream is= getClass().getResourceAsStream(filePath);
			InputStream is= new FileInputStream(new File(filePath));
			load(is);
		} catch (IOException e) {
			logger.error(LogConstants.MESSAGE_PROPERTIES_READ_ERROR,filePath);
			logger.error(e.getMessage(),e.fillInStackTrace());
		}
	}
	
	private static class SingletonContainer{
		private static final String path = PathProperties.getInstance().getProperty("project.path");
		private static ProjectProperties instance = new ProjectProperties(path);
	}

	/**
	 * Searches for the property with the specified key in this property list.
	 * If the key is not found in this property list, the default property list,
	 * and its defaults, recursively, are then checked. The method returns
	 * <code>null</code> if the property is not found.
	 *
	 * @param key the property key.
	 * @return the value in this property list with the specified key value.
	 * @see #setProperty
	 * @see #defaults
	 */
	@Override
	public String getProperty(String key) {
		try {
			return super.getProperty(key);
		}catch (Exception e){
			logger.error(e.getMessage(),e.fillInStackTrace());
			return null;
		}
	}

	public static ProjectProperties getInstance(){
		return SingletonContainer.instance;
	}
}
